package com.py.credit.controller;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.DataCoverageService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 功能描述: 数据覆盖率统计
 *
 * @author Lxw
 * @date 19/4/4 10:21
 */
@RestController
@RequestMapping(value = "/dataResult")
public class DataCoverageController extends BaseController {

    @Autowired
    private DataCoverageService dataCoverageService;

    /**
     * 功能描述: 覆盖率统计定时任务
     *
     * @author YaoS
     * @date 19/4/8 18:24
     */
    @IgnoreAuthority
    @RequestMapping(value = "/insertData.do")
    public RestServiceResult<Object> insertData() {
        RestServiceResult<Object> r = new RestServiceResult<>();
        try {
            dataCoverageService.insertData();
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg(e.toString());
            ExceptionLogger.error("覆盖数据插入失败异常！", e.getMessage(), e);
        }
        return r;
    }

    /**
     * 功能描述: 查询覆盖率总信息
     *
     * @return 查询结果
     * @author YaoS
     * @date 19/4/9 11:22
     */
    @IgnoreAuthority
    @RequestMapping(value = "/selectInsertData.json")
    public RestServiceResult<List<Map<String, Object>>> selectInsertData() {
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<>();
        try {
            r.setData(dataCoverageService.selectInsertDataCount());
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg(e.toString());
        }
        return r;
    }

    /**
     * 功能描述: 根据id查询t_data_table详情
     *
     * @param tableId 资源id
     * @return 查询结果
     * @author YaoS
     * @date 19/4/9 11:28
     */
    @RequestMapping(value = "/selectInsertColumnTable.json")
    public RestServiceResult<List<Map<String, Object>>> selectInsertColumnTable(
            @RequestParam(defaultValue = "", value = "tableId") String tableId) {
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<>();
        int i = 0;
        try {
            if (!StringUtils.isEmpty(tableId)) {
                i = Integer.valueOf(tableId);
            }
            r.setData(dataCoverageService.selectInsertColumnTable(i));
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg(e.toString());
        }
        return r;
    }

    /**
     * 功能描述: 分页查询t_data_table
     *
     * @param page 分页信息
     * @return 查询结果
     * @author YaoS
     * @date 19/4/9 11:28
     */
    @RequestMapping(value = "/selectInsertColumnTableForPage.json")
    public RestServiceResult<List<Map<String, Object>>> selectInsertColumnTableForPage(Page page) {
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<>();
        try {
            r.setData(dataCoverageService.selectInsertColumnTableForPage(page));
            r.setCount(page.getTotalCount());
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg(e.toString());
        }
        return r;
    }

    /**
     * 功能描述: 分页查询资源信息详情数据
     *
     * @param page    分页条件
     * @param tableId 资源id
     * @return 查询结果
     * @author YaoS
     * @date 19/4/9 11:31
     */
    @RequestMapping(value = "/selectInsertDeptCountForPage.json")
    public RestServiceResult<List<Map<String, Object>>> selectInsertDeptCountForPage(Page page, String tableId) {
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<>();
        try {
            if (!StringUtils.isEmpty(tableId)) {
                r.setData(dataCoverageService.selectInsertDeptCountForPage(page, Integer.valueOf(tableId)));
                r.setCount(page.getTotalCount());
            } else {
                r.setCode(RestServiceResult.CODE_UNKNOW);
                r.setMsg("查询数据错误");
            }
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg(e.toString());
        }
        return r;
    }
}
